package com.jiazhong.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import java.io.IOException;

// 三种实现方式
@WebFilter(value = "/*", servletNames = {"first"}, initParams = {
        @WebInitParam(name = "myname", value = "admin101", description = "我是一个filter")
})
public class FirstFilter implements Filter {
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("FirstFilter doFilter");
        String name = req.getParameter("name");
        if ("admin".equals(name)) {
            // 放行
            filterChain.doFilter(req, resp);
        }
        System.out.println("FirstFilter end");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("FirstFilter init");
        String myname = filterConfig.getInitParameter("myname");
        System.out.println(myname);
    }

    @Override
    public void destroy() {
        System.out.println("FirstFilter destroy");
    }
}
